/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bizfty.apiexecutor;

import com.bizfty.apiexecutor.classloader.InMemoryJavaCompiler;
import com.sun.codemodel.CodeWriter;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JPackage;
import com.sun.tools.xjc.ConsoleErrorReporter;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.tools.JavaFileObject.Kind;
import org.xml.sax.InputSource;

/**
 *
 * @author johns
 */
public class GenClassFromXsd {

    public static void main(String[] args) throws IOException, Exception {
        GenClassFromXsd tool = new GenClassFromXsd();
        tool.textGenClass();
    }

    public void textGenClass() throws IOException, Exception {
        SchemaCompiler sc = XJC.createSchemaCompiler();
        ErrorListener elForRun = new ConsoleErrorReporter();
        sc.setErrorListener(elForRun);
        String filePath = this.getClass().getClassLoader().getResource("schema4.xsd").getPath();
        InputSource file = new InputSource(filePath);
        sc.parseSchema(file);
        S2JJAXBModel model = sc.bind();
        JCodeModel jcm = model.generateCode(null, null);
        Map<String,String> classSource = new HashMap<>();
        jcm.build(new CodeWriter(){
            public OutputStream openBinary(JPackage pkg, String fileName) throws IOException {
                final String name = pkg != null && pkg.name().length() > 0 ? pkg.name() + '.' + fileName : fileName;
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                return new FilterOutputStream(baos) {
                    @Override
                    public void close() throws UnsupportedEncodingException {
                        // don't let this stream close
                        classSource.put(name, baos.toString("GBK"));
                    }
                };
            }
            @Override
            public void close() throws IOException {
            }
        });
        InMemoryJavaCompiler imc = InMemoryJavaCompiler.newInstance();
        for(String k : classSource.keySet()){
            String name = k.replaceAll(Kind.SOURCE.extension,"");
            imc.addSource(name, classSource.get(k));
            System.out.println("name:"+k);
            System.out.println("-----------------------");
            System.out.println(classSource.get(k));
        }
        Map<String, Class<?>> clazz = imc.compileAll();
        System.out.println(clazz);
    }
}
